//
//  HOBigImageViewController.m
//  Huahai
//
//  Created by Fox Guo on 4/8/14.
//
//

#import "HOBigImageViewController.h"
#import "HOUtils.h"
@implementation CustomOverlayView
- (id)initWithFrame:(CGRect)frame
{
	if(self = [super initWithFrame:frame])
	{
		pageIndexLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
		[self addSubview:pageIndexLbl];
		[pageIndexLbl setBackgroundColor:[UIColor clearColor]];
		pageIndexLbl.textColor = [UIColor blackColor];
		pageIndexLbl.textAlignment = NSTextAlignmentCenter;
		[pageIndexLbl setFont:[UIFont boldSystemFontOfSize:16]];
		self.backgroundColor = [UIColor whiteColor];
		self.layer.cornerRadius = 5.0f;
		self.alpha = 0.4;
	}
	return self;
}
- (void)setCurrentIndex:(NSInteger)currentIndex total:(NSInteger)total
{
	[pageIndexLbl setText:[NSString stringWithFormat:@"%ld/%ld", (long)currentIndex, (long)total]];
}

@end
@interface HOBigImageViewController ()
{
	HOBigImageViewer *bigImageViewer;
	BOOL isSheetOpened;
	CustomOverlayView *overlayView;
}
@property(nonatomic, retain)UIImage *imgData;
@end

@implementation HOBigImageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithImages:(NSArray *)array withStartsIndex:(int)startIndex
{
	self = [super init];
	if(self)
	{
		self.imagesData = array;
		self.currentIndex = startIndex;
	}
	return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    bigImageViewer = [[HOBigImageViewer alloc] initWithFrame:CGRectMake(0,
																			  0,
																			  [HOUtils getScreenWidth],
																			  [HOUtils getScreenHeight])
															andData:self.imagesData
															andImageindex:self.currentIndex];
	bigImageViewer.delegate = self;
    [bigImageViewer setBackgroundColor:[UIColor blackColor]];
    [self.view setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:bigImageViewer];
	overlayView = [[CustomOverlayView alloc]initWithFrame:CGRectMake(([HOUtils getScreenWidth] - 100) / 2,
																	 [HOUtils getScreenHeight] - 50,
																	 100,
																	 35)];
	[self updateCurrentIndex:(self.currentIndex + 1) total:[self.imagesData count]];
	[self.view addSubview:overlayView];
}

- (void)updateCurrentIndex:(NSInteger)index total:(NSInteger)total
{
	[overlayView setCurrentIndex:index total:total];
}
- (void)dealloc
{
	bigImageViewer.delegate = nil;
    [bigImageViewer release];
	self.imagesData = nil;
	self.imgData = nil;
    [super dealloc];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    bigImageViewer.delegate = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    isSheetOpened = NO;}
- (void)viewWillDisappear:(BOOL)animated
{
    isSheetOpened = NO;
	self.navigationController.navigationBar.hidden = YES;
    [super viewWillDisappear:animated];
}
- (BOOL)prefersStatusBarHidden {
    return YES;
}
- (void)imageViewerDidClose
{
    [self dismissViewControllerAnimated:NO completion:nil];
//    [[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
//    [[self navigationController] setNavigationBarHidden:NO];
//    [UIView animateWithDuration:.3f
//                          delay:0
//                        options:(UIViewAnimationOptionAllowUserInteraction|
//                                 UIViewAnimationOptionBeginFromCurrentState)
//                     animations:^(void) {
//                         self.view.alpha = 0.0f;
//                     }
//                     completion:^(BOOL finished) {
//                         self.view.alpha = 0.0f;
//                         [self dismissViewControllerAnimated:YES completion:nil];
//                     }];
    
    
}

- (void)longPress:(UILongPressGestureRecognizer *)longPress withImageData:(UIImage *)imageData
{
    if(isSheetOpened)
        return;
    self.imgData = imageData;
    isSheetOpened = YES;
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:nil
                                  delegate:self
                                  cancelButtonTitle:NSLocalizedString(@"取消", @"取消")
                                  destructiveButtonTitle:NSLocalizedString(@"保存图片至相册", @"保存图片至相册")
                                  otherButtonTitles:nil,nil];
    [actionSheet showInView:self.view];
    
    [actionSheet release];
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    isSheetOpened = NO;
    if (self.imgData != nil && buttonIndex == 0) {
        UIImageWriteToSavedPhotosAlbum(self.imgData, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (!error) {
        [self popupHintView:NSLocalizedString(@"保存成功!", @"保存成功!")];
    }
    else
    {
        
        [self popupHintView:NSLocalizedString(@"保存失败!", @"保存失败!")];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
